package com.xty.xtyle;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.text.TextUtils;

import com.ashokvarma.bottomnavigation.BottomNavigationBar;
import com.ashokvarma.bottomnavigation.BottomNavigationItem;
import com.xty.xtyle.db.ClothTypes;
import com.xty.xtyle.db.ClothTypesDao;
import com.xty.xtyle.db.mix.MixType;
import com.xty.xtyle.db.mix.MixTypeDao;
import com.xty.xtyle.frames.CanFragment;
import com.xty.xtyle.frames.HomeFragment;
import com.xty.xtyle.frames.MeFragment;
import com.xty.xtyle.frames.ClothFragment;
import com.xty.xtyle.frames.MixFragment;

import org.litepal.LitePal;

public class MainActivity extends AppCompatActivity implements BottomNavigationBar.OnTabSelectedListener {


    //首页底部栏
    private BottomNavigationBar bottomNavigationBar;
    private FragmentManager fm;

    //首页底部控制的页面
    private HomeFragment homeFragment;
    //衣柜
    private ClothFragment clothFragment;
    //混搭
    private MixFragment mixFragment;
    //日历
    private CanFragment canFragment;
    //个人中心
    private MeFragment meFragment;

    private int curPosition;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
        setDefaultFragment();
        LitePal.getDatabase();
        initType();
        initMixType();

    }

    /**
     * 默认给数据库添加这几种一个柜内容
     */
    void initType() {
        String[] types = {"上衣","下装","鞋子","裙子","包","配件","外套"};
        ClothTypesDao clothTypesDao = new ClothTypesDao();
        for (String type : types) {
            ClothTypes clothTypes = new ClothTypes(type);
            clothTypesDao.add(clothTypes);
        }
    }

    /**
     * 默认添加搭配：场景
     */
    void initMixType() {
        String[] types = {"日常","工作","度假","最爱"};
        MixTypeDao mixTypeDao = new MixTypeDao();
        for (String type : types) {
            MixType mixType = new MixType(type);
            mixTypeDao.add(mixType);
        }
    }

    /**
     * 初始化底部导行栏按钮，并默认显示第一个首页
     */
    private void initView() {
        bottomNavigationBar = (BottomNavigationBar) findViewById(R.id.bottom_navigation_bar);
        /*
         * 设置模式
         * MODE_DEFAULT:
         * 如果Item的个数<=3就会使用MODE_FIXED模式，否则使用MODE_SHIFTING模式
         *
         * MODE_FIXED
         * 填充模式，未选中的Item会显示文字，没有换挡动画。
         *
         * MODE_SHIFTING
         * 换挡模式，未选中的Item不会显示文字，选中的会显示文字。在切换的时候会有一个像换挡的动画
         */
        bottomNavigationBar.setMode(BottomNavigationBar.MODE_FIXED);

        bottomNavigationBar.setBackgroundStyle(BottomNavigationBar.BACKGROUND_STYLE_RIPPLE);
        //注册切换监听时间
        bottomNavigationBar.setTabSelectedListener(this);
        bottomNavigationBar.addItem(new BottomNavigationItem(R.mipmap.home, "主页"))
                .addItem(new BottomNavigationItem(R.mipmap.news, "衣柜"))
                .addItem(new BottomNavigationItem(R.mipmap.office, "搭配"))
                .addItem(new BottomNavigationItem(R.mipmap.can, "日历"))
                .addItem(new BottomNavigationItem(R.mipmap.me, "个人"))
                //不触发事件
                .setFirstSelectedPosition(0)
                //进行渲染，所有设置属性后都需要调用该方法
                .initialise();

    }

    private void setDefaultFragment() {
        fm = getSupportFragmentManager();
        FragmentTransaction transaction = fm.beginTransaction();
        homeFragment = (HomeFragment) Fragment.instantiate(this, HomeFragment.class.getName());
        transaction.add(R.id.ll_content, homeFragment);
        transaction.commit();
    }

    /**
     * 隐藏界面
     * @param ft
     */
    private void hideFragment(FragmentTransaction ft) {
        if (homeFragment != null) {
            ft.hide(homeFragment);
        }
        if (clothFragment != null) {
            ft.hide(clothFragment);
        }
        if (mixFragment != null) {
            ft.hide(mixFragment);
        }
        if (meFragment != null) {
            ft.hide(meFragment);
        }
        if (canFragment != null) {
            ft.hide(canFragment);
        }
    }

    /**
     * 当前面底部item选择，显示具体内容
     * @param position
     */
    @Override
    public void onTabSelected(int position) {
        curPosition = position;
        FragmentTransaction ft = fm.beginTransaction();
        hideFragment(ft);
        switch (position) {
            case 0:
                if (homeFragment == null) {
                    homeFragment = (HomeFragment) Fragment.instantiate(this, HomeFragment.class.getName());
                    ft.add(R.id.ll_content, homeFragment);
                } else {
                    ft.show(homeFragment);
                }
                break;
            case 1:
                if (clothFragment == null) {
                    clothFragment = (ClothFragment) Fragment.instantiate(this, ClothFragment.class.getName());
                    ft.add(R.id.ll_content, clothFragment);
                } else {
                    ft.show(clothFragment);
                }
                break;
            case 2:
                if (mixFragment == null) {
                    mixFragment = (MixFragment) Fragment.instantiate(this, MixFragment.class.getName());
                    ft.add(R.id.ll_content, mixFragment);
                } else {
                    ft.show(mixFragment);
                }
                break;

            case 3:
                if (canFragment == null) {
                    canFragment = (CanFragment) CanFragment.instantiate(this, CanFragment.class.getName());
                    ft.add(R.id.ll_content, canFragment);
                } else {
                    ft.show(canFragment);
                }
                break;
            case 4:
                if (meFragment == null) {
                    meFragment = (MeFragment) Fragment.instantiate(this, MeFragment.class.getName());
                    ft.add(R.id.ll_content, meFragment);
                } else {
                    ft.show(meFragment);
                }
                break;
        }
        ft.commit();
    }

    @Override
    public void onTabUnselected(int position) {

    }

    @Override
    public void onTabReselected(int position) {

    }

    /**
     * HomeFragment页面的，四个页面的点击切换界面
     * 点击他们，类似跳转底部item
     * @param p
     */
    public void shiftFragment(int p) {
        FragmentTransaction ft = fm.beginTransaction();
        hideFragment(ft);
        curPosition = p;
        bottomNavigationBar.selectTab(curPosition);
        switch (p) {
            case 1:
                if (clothFragment == null) {
                    clothFragment = (ClothFragment) Fragment.instantiate(this, ClothFragment.class.getName());
                    ft.add(R.id.ll_content, clothFragment);
                } else {
                    ft.show(clothFragment);
                }
                break;
            case 2:
                if (mixFragment == null) {
                    mixFragment = (MixFragment) Fragment.instantiate(this, MixFragment.class.getName());
                    ft.add(R.id.ll_content, mixFragment);
                } else {
                    ft.show(mixFragment);
                }
                break;
            case 3:
                if (canFragment == null) {
                    canFragment = (CanFragment) CanFragment.instantiate(this, CanFragment.class.getName());
                    ft.add(R.id.ll_content, canFragment);
                } else {
                    ft.show(canFragment);
                }
                break;
        }
        ft.commit();
    }
}
